package Rtype;

import java.awt.Image;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Ship{

    private String ship = "../resources/ship.gif";

    private int dx;
    private int dy;
    private int x;
    private int y;
    private Image image;

    private ArrayList<Shot> missiles;
    
    public Ship() {
        ImageIcon ii = new ImageIcon(this.getClass().getResource(ship));
        image = ii.getImage();
        x = 40;
        y = 60;
        missiles = new ArrayList<Shot>();
    }

    public ArrayList<Shot> getMissiles() {
		return missiles;
	}

    public void move() {
        x += dx;
        y += dy;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Image getImage() {
        return image;
    }

    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();
        
        if(key == KeyEvent.VK_SPACE){
        	fire();
        }

        if (key == KeyEvent.VK_LEFT) {
            dx = -1;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 1;
        }

        if (key == KeyEvent.VK_UP) {
            dy = -1;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 1;
        }
    }

    private void fire() {
		missiles.add(new Shot(x + Constants.SHIP_SIZE, y + Constants.SHIP_SIZE/2));
	}

	public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }
}